// Every mission is created on the server only, if needed, synchronization with clients is done through events.
// But we load the mission file on the client aswell in order to have Mission Specific data like Dialogs available on client side aswell. Otherwise we need to send them over the network. 
private ["_mission_id","_missions","_selected_mission","_mission_file","_mission_params","_current_mission_id","_start_wait","_player"];
_mission_id = _this select 0;
if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: Input: %3",diag_tickTime,"launch_mission.sqf",_this]; };
If (_mission_id < 0) exitWith {diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Invalid mission ID!",_this];};
_current_mission_id = ["current_mission_id",-1] call XNetGetJIP; 
_missions = ["available_missions",[]] call XNetGetJIP;
if (count _missions <= 0) exitWith {diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","There are no available missions!",_this];};

if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: Ids: %3, %4",diag_tickTime,"launch_mission.sqf",_mission_id, _current_mission_id]; };
If (X_Server) then {
	If (_mission_id == _current_mission_id) exitWith {diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","The mission is already running!",_this];};
	
	If (isNil "DM_Mission_Last_Launch") then { DM_Mission_Last_Launch=-1000 };
	If ((Time - DM_Mission_Last_Launch) < 30) exitWith {
		diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Need to wait between two missions",(Time - DM_Mission_Last_Launch)];
		Format["You need to wait %1 seconds before launching a mission!",ceil(Time - DM_Mission_Last_Launch)] call XfMissionMessageToAll;
	};
	DM_Mission_Last_Launch = Time;
	
	diag_log "";
	diag_log " =================== LAUNCHING MISSION =================== ";
	diag_log "";
	diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Script arguments",_this];
	_mission_params = [];
	If (count _this > 1) then {	_mission_params = _this select 1; };
	_player = objNull;
	If (count _this > 2) then {	_player = _this select 2; };
	If (! isNull _player) then { ["d_msg",[Format["%1 selected a new mission!",name(_player)],"all",objNull,"hq"]] call XNetCallEvent; };
	
	_missions = ["available_missions",[]] call XNetGetJIP;
	if (_mission_id >= (count _missions)) exitWith {diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Mission ID out of range",_this];};
	
	_selected_mission = _missions select _mission_id;
	_mission_file = _selected_mission select 0;
	
	If (isNil "DM_Mission_Mission_Runs") then {DM_Mission_Mission_Runs = False};
	_current_mission_id = ["current_mission_id",_mission_id] call XNetGetJIP; 
	if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4 (%5)",diag_tickTime,"launch_mission.sqf","Running mission",_current_mission_id,DM_Mission_Mission_Runs]; };
	if (DM_Mission_Mission_Runs) then {
		if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Cancelling running mission",DM_Mission_Name]; };
		_startWait = time;
		While {DM_Mission_Mission_Runs AND ((time - _startWait) < 60)} do {
			DM_Mission_Mission_CanRun = False;
			DM_Mission_Chapter_CanRun = False;
			["current_mission_completed",true] call XNetSetJIP; 
			sleep 1;
		};
		if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Running mission canceled",DM_Mission_Name]; };
		sleep 5;
		// Just in case the current mission crashed for a reason or another, we clean it up. Normally it has already been done... but yeah... just in case :)
		// This way you can ensure that the next one will start properly.
		DM_Mission_Name="";
		[] call XfMissionCleanAll;
		[] call XfMissionCloseMission;
		sleep 10;
	};
	DM_Mission_Name="";
	if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Loading",_mission_file]; };
	call compile preprocessFileLineNumbers _mission_file;
	if (DM_Mission_Name != "") then {	
		["current_mission_id",_mission_id] call XNetSetJIP; 
		["current_mission_completed",false] call XNetSetJIP;
		["current_mission_position",[[0,0,0],0,""]] call XNetSetJIP;
		_mission_params spawn XfMissionExecute;
	};
};
If (X_Client && !X_Server) then {
	if (_mission_id >= 0) then {
		_startWait = time;
		While {(_mission_id != _current_mission_id) AND ((time - _startWait) < 120)} do {
			_current_mission_id = ["current_mission_id",-1] call XNetGetJIP; 
			sleep 1;
		};
		_selected_mission = _missions select _current_mission_id;
		_mission_file = _selected_mission select 0;
		if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,"launch_mission.sqf","Loading",_mission_file]; };
		call compile preprocessFileLineNumbers _mission_file;
	};
};